<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use \Common\Model\Goods;
use \Common\Model\GoodsAttr;
use \Common\Model\Cate;
//测试控制器
class ShopCarController extends Controller{   
    public function index(){
    	//判断用是否登录
		if(isset($_SESSION['user'])){
			$user = $_SESSION['user'];
			View::with('user',$user);							
		}	

    	//获取购物车商品数据
    	$goodsCarData = Cart::getGoods();
		//判断购物车中是否有商品
		if($goodsCarData){
			//获取购物车中最后商品的cid 
			foreach($goodsCarData as $k=>$v){
				$cateCid = $v['cid'];
			}
			$cateModel = new Cate; 
			//获取该商品分类的上一级分类
			$cateCid = $cateModel->where('cid',$cateCid)->pluck('pid');	
			View::with('cateCid',$cateCid);						
		}
		//统计购物车中的商品数量
		$goodsCarNum = Cart::getTotalNums();
		//获得商品总价格
		$goodsCarPrice = Cart::getTotalPrice();
		$goodsCarPrice = number_format($goodsCarPrice,2);		

		View::with('goodsCarData',$goodsCarData);		
		View::with('goodsCarNum',$goodsCarNum);		
		View::with('goodsCarPrice',$goodsCarPrice);						
		View::make();
	}
	
	// 购物车添加   
    public function add(){
    	//1.获取商品id
    	$gid = q('post.gid');
		$goodsModel = new  Goods;
		$goodsinfo = $goodsModel->where('gid',$gid)->find();
		//2.获取购买商品数量
		$goodsNum = q('post.gn');
    	//3.获取存入购物车商品信息
    	$combine = q('post.str');
		//去掉字符串最后一个字符 (,)
		$newCombine = substr($combine, 0,strlen($combine)-1);
		/*获取商品规格*/
		//连接商品属性和属性类型表
		$goodsAttr = Db::table('goods_attr ga')
							->join('type_attr ta','ga.type_attr_trid','=','ta.trid')
							->where("goods_gid={$gid} AND gaid IN({$newCombine})")
							->get();											
		//组合商品规格
		foreach($goodsAttr as $k=>$v){
			$options{$v['trname']} = $v['gvalue'];
		}
		//购物车添加 
        $data = [
            'id'        => $gid, 					//商品 ID
            'name'      => $goodsinfo['gname'],		//商品名称
            'num'       => $goodsNum, 				//商品数量
            'cid'		=> $goodsinfo['cate_cid'],	//商品cid
            'price'     => $goodsinfo['mallPrice'], //商品价格
            'picture'	=> $goodsinfo['pic'],		//商品图片
            'options'   => $options,				//商品规格
            'combine'   => $newCombine 				//属性组合id，用于订单列表
            ];
        Cart::add($data);
		//得到总数传到前台
		$goodsData = [
			'num'       => cart::getTotalNums(), // 购物车商品总数量
			'goodsprice'  => cart::getTotalPrice(),// 商品总价格
		];
		echo json_encode($goodsData);exit;
       		
    }

	//购买数量变化
	public function editNum(){
	    //1.获取商品id
    	$sid = q('post.sid');
		//2.获取购买商品数量
		$goodsNum = q('post.gn');

		//更新购物车
		$data=array( 
		    'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
		    'num'=>$goodsNum
		);
		Cart::update($data);
		
		$data = [
		    'sid'=>$sid,
		    'num'=>$goodsNum,
		    'TotalNums'=>Cart::getTotalNums(), 
		    'totalPrice'=>Cart::getTotalPrice()
		];
		echo json_encode($data);exit;
	}
	
	//购物车 删除
	public function del(){
	   	$sid = q('get.sid');
		//更新购物车
		$data=array( 
		    'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
		    'num'=>0 
		); 
		Cart::update($data);
		go(U('ShopCar/index',['cid'=>$_GET['cid']]));
	}

	//异步 下单跳转
	public function shop(){
		//判断购物车中是否有商品，若没有则返回2
		if($_SESSION['cart']['goods']){
			echo json_encode(1);exit;
		}else{
			echo json_encode(2);exit;
		}	
	}
}
